package com.eam.handler;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.openbravo.client.kernel.BaseActionHandler;
import org.openbravo.dal.service.OBDal;
import java.util.Map;

public class EmesoFacilitiesCollectionHandler extends BaseActionHandler {
	public static final String FACILITY = "Facility"; //设备
	public static final String MAINTAIN = "Maintain"; //计划
	public static final String OBPOPWINDOWPLAN = "OBPOPWINDOWPLAN"; //弹出框(选择计划打开)
	public static final String OBPOPWINDOWITEM = "OBPOPWINDOWITEM"; //弹出框(选择设备打开)
	public static final String OKBUTTON = "OKBUTTON"; //OK按钮
	public static final String REPAIR = "REPAIR"; //维修
    private EmesoFacilitiesCollectionHandlerService ec = new EmesoFacilitiesCollectionHandlerService();
	
	@Override
	protected JSONObject execute(Map<String, Object> parameters, String data) {
		JSONObject jsonRep = new JSONObject();
		try {
			JSONObject jsonData = new JSONObject(data);
			
			String type = jsonData.getString("type");
			
			if(OKBUTTON.equalsIgnoreCase(type)){
				String recordID = jsonData.getString("code");
				String actionType = jsonData.getString("action");
				ec.confirmMaintainPlan(recordID, actionType);
				jsonRep.put("flag", true);
				jsonRep.put("message", "处理成功");
			}else{
				String recordID = jsonData.getString("code");
				jsonRep = ec.getPlanItem(recordID,type);
			}
			OBDal.getInstance().commitAndClose();
		} catch (Exception e) {
			OBDal.getInstance().rollbackAndClose();
			try {
				jsonRep.put("result", false);
				jsonRep.put("msg", "处理失败！");
			} catch (JSONException e1) {
				e.printStackTrace();
			}
		}
		return jsonRep;
	}
}
